//
//  PongField.mm
//
//  Created by Jonathan Saggau on 2007-08-09.
//  Copyright (c) 2007 __MyCompanyName__. All rights reserved.
//

#import "PongField.h"
#import <UIKit/UIKit.h>
#import "Common.h"

@implementation PongField

-(id)initWithFieldRect:(CGRect)aFieldRect;
{
	self = [super init];
	if (nil != self)
	{
		fieldRect = aFieldRect;
		leftFinger = CGPointMake(0.0, 0.0);
		rightFinger = CGPointMake(0.0, 0.0);
		leftPaddle.size = CGSizeMake(50.0, 10.0);
		leftPaddle.origin.x = ((fieldRect.size.width * 0.5) - (leftPaddle.size.width *.5));
		leftPaddle.origin.y = 0.0;
		
		rightPaddle.size = leftPaddle.size;
		rightPaddle.origin.x = leftPaddle.origin.x;
		rightPaddle.origin.y = fieldRect.size.height - 10.0;
		ballRect.size = CGSizeMake(10.0, 10.0);
		ballRect.origin.x = (fieldRect.size.height * 0.5) - (ballRect.size.height * 0.5);
		ballRect.origin.y = (fieldRect.size.width * 0.5) - (ballRect.size.width * 0.5);
		
		leftScore = 0;
		rightScore = 0;
		
		color[0] = 0.0;
		color[1] = 1.0;
		color[2] = 0.0;
		color[3] = 1.0;
	}
	return self;
}

- (id)init
{	
	struct CGRect rect = [UIHardware fullScreenApplicationContentRect];
	return [self initWithFieldRect:rect];
}

-(void)dealloc
{
	[super dealloc];
}

- (void)drawBackgroundInContext:(CGContextRef)ctx
{
	CGContextSetFillColor(ctx, color);
    CGContextSetStrokeColor(ctx, color);
    
    //Field
	CGContextBeginPath(ctx);
	
	CGContextSetStrokeColor(ctx, color);
	CGContextSetLineWidth( ctx, 5.0 );
	CGContextAddRect (ctx, fieldRect);
	CGContextDrawPath(ctx,  kCGPathStroke);
	
	// play with line dashes
	CGContextBeginPath(ctx);
	float dashPhase = 1.0;
    float dashLength = fieldRect.size.width / 16.0;
	float dashLengths[] = {(dashLength * 2), dashLength};
	CGContextSetLineDash( ctx,
	dashPhase, dashLengths,
	sizeof( dashLengths ) / sizeof( float ) );
	
	float midY = (fieldRect.origin.y + fieldRect.size.height *.50);
	CGContextMoveToPoint( ctx, fieldRect.origin.x + 20.0, midY);
	CGContextAddLineToPoint( ctx, (fieldRect.origin.x + fieldRect.size.width - 20), midY );
	CGContextStrokePath( ctx );   
}

//needs some optim.
-(void)drawInContext:(CGContextRef)ctx
{
	CGContextSetFillColor(ctx, color);
    CGContextSetStrokeColor(ctx, color);
    
//txt
//http://developer.apple.com/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_text/chapter_18_section_4.html
    float moveThisMuch = fieldRect.size.height/12;
    CGContextSelectFont (ctx, "DB_LCD_Temp-Black",  moveThisMuch, kCGEncodingMacRoman);

    CGContextSetCharacterSpacing (ctx, 8);
    CGContextSetTextDrawingMode (ctx, kCGTextFillStroke);

    CGAffineTransform myTextTransform = CGAffineTransformMakeScale(1.0, -1.0);
    CGContextSetTextMatrix (ctx, myTextTransform);

    NSString *score = [NSString stringWithFormat:@"%i", [self rightScore]];

    CGPoint scorePoint = fieldRect.origin;
    scorePoint.x += moveThisMuch;
    scorePoint.y += moveThisMuch*2;
    CGContextShowTextAtPoint(ctx,  scorePoint.x, scorePoint.y, [score cString], [score length]);
    
    score = [NSString stringWithFormat:@"%i", [self leftScore]];    
    scorePoint.y = [self fieldRect].size.height - moveThisMuch;
    CGContextShowTextAtPoint(ctx,  scorePoint.x, scorePoint.y, [score cString], [score length]); 
    
//Paddles and ball
	CGContextFillRect(ctx, leftPaddle);
	CGContextFillRect(ctx, rightPaddle);
	CGContextFillRect(ctx, ballRect);


}


//accessors
//=========================================================== 
//  fieldRect 
//=========================================================== 
- (CGRect)fieldRect
{
    return fieldRect;
}
- (void)setFieldRect:(CGRect)aFieldRect
{
    fieldRect = aFieldRect;
}

//=========================================================== 
//  leftFinger 
//=========================================================== 
- (CGPoint)leftFinger
{
    return leftFinger;
}
- (void)setLeftFinger:(CGPoint)aLeftFinger
{
    leftFinger = aLeftFinger;
}

//=========================================================== 
//  rightFinger 
//=========================================================== 
- (CGPoint)rightFinger
{
    return rightFinger;
}
- (void)setRightFinger:(CGPoint)aRightFinger
{
    rightFinger = aRightFinger;
}

//=========================================================== 
//  leftPaddle 
//=========================================================== 
- (CGRect)leftPaddle
{
    return leftPaddle;
}
- (void)setLeftPaddle:(CGRect)aLeftPaddle
{
    leftPaddle = aLeftPaddle;
}

//=========================================================== 
//  rightPaddle 
//=========================================================== 
- (CGRect)rightPaddle
{
    return rightPaddle;
}
- (void)setRightPaddle:(CGRect)aRightPaddle
{
    rightPaddle = aRightPaddle;
}

//=========================================================== 
//  leftScore 
//=========================================================== 
- (int)leftScore
{
    return leftScore;
}
- (void)setLeftScore:(int)aLeftScore
{
    leftScore = aLeftScore;
}

//=========================================================== 
//  rightScore 
//=========================================================== 
- (int)rightScore
{
    return rightScore;
}
- (void)setRightScore:(int)aRightScore
{
    rightScore = aRightScore;
}

//=========================================================== 
//  ballRect 
//=========================================================== 
- (CGRect)ballRect
{
    return ballRect;
}
- (void)setBallRect:(CGRect)aBallRect
{
    ballRect = aBallRect;
}

- (void)setColorRed:(float)r green:(float)g blue:(float)b alpha:(float)a;
{
	color[0] = r;
	color[1] = g;
	color[2] = b;
	color[3] = a;
}

@end
